package com.example.braden.answergame.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;
import com.example.braden.answergame.R;
import com.example.braden.answergame.common.Constant;
import com.example.braden.answergame.common.LogUtils;
import com.example.braden.answergame.common.utils.MediaUtil;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

/**
 * Created by braden on 2018/3/19.
 */

public class CountdownView extends android.support.v7.widget.AppCompatTextView {

    private Disposable disposable;
    private long time;
    private OnCountdownListener countdownListener;
    private long current;
    private boolean isLong;//分为10秒倒计时和5秒倒计时
    private Context mContext;

    public CountdownView(Context context) {
        super(context);
        this.mContext = context;
    }

    public CountdownView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public void setCountdownListener(OnCountdownListener listener) {
        this.countdownListener = listener;
    }

    public void start(int time1) {
        this.time = time1;
        isLong = time1 == 10;
        Observable.interval(0, 1000, TimeUnit.MILLISECONDS)
                .take(time + 1)
                .map(aLong -> time - aLong)
                //.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override public void onSubscribe(Disposable d) {
                        disposable = d;
                        LogUtils.e("倒计时：onSubscribe");
                    }

                    @Override public void onNext(Long o) {
                        current = o;
                        setText(o + "");
                        countdownListener.onProgress(o);
                        LogUtils.e("倒计时：onProgress:" + o);
                    }

                    @Override public void onError(Throwable e) {
                        LogUtils.e("倒计时：onError");
                    }

                    @Override public void onComplete() {
                        disposable.isDisposed();
                        LogUtils.e("倒计时：onComplete");
                    }
                });
    }

    public boolean isLong() {
        return isLong;
    }

    public void setLong(boolean aLong) {
        isLong = aLong;
    }

    public long getTime() {
        return current;
    }

    public void stop() {
        if (disposable != null) {
            disposable.dispose();
        }
    }

    public interface OnCountdownListener {
        void onProgress(long progress);
    }
}
